<template>
  <div>
    <div><h1>品牌分页列表</h1></div>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入品牌名称"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="loadList">搜索</el-button>
          <el-button type="primary" @click="onAddBrand">添加品牌</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <div>
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="brandId" label="ID" width="50" />
          <el-table-column prop="brandName" label="品牌名称" width="100" />
          <el-table-column prop="brandUse" label="数量" width="100" />
          <el-table-column prop="brandTop" label="排序" width="100" />
          <el-table-column prop="brandStatus" label="品牌状态" width="150">
            <template #default="scope">
              <el-switch
                v-model="scope.row.brandStatus"
                :active-value="1"
                :inactive-value="0"
                active-text="显示"
                inactive-text="隐藏"
                @change="getStatus(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column prop="createBy" label="创建者" width="120" />
          <el-table-column prop="createTime" label="创建时间" width="100" />
          <el-table-column label="操作" width="100" />
        </el-table>
      </div>
      <div>
        <div class="demo-pagination-block">
          <el-pagination
            v-model:current-page="searchForm.pageNum"
            v-model:page-size="searchForm.pageSize"
            :page-sizes="[2, 4, 6, 8]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <div>
      <el-dialog
        v-model="dialogFormVisible"
        title="Shipping address"
        width="500"
      >
        <el-form :model="addForm" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="品牌名称"
            :label-width="formLabelWidth"
            prop="brandName"
          >
            <el-input v-model="addForm.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input v-model="addForm.brandTop" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="brandStatus"
          >
            <el-radio-group v-model="addForm.brandStatus">
              <el-radio :value="1">显示</el-radio>
              <el-radio :value="0">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">Cancel</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              保存
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import { getBrandList, getAddBrand, updateStatus } from "../api/api";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const tableData = ref([]);
const total = ref(0);

const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});
const ruleFormRef = ref<FormInstance>();
const addForm = ref({
  brandId: null,
  brandName: "",
  brandStatus: null,
  brandTop: null,
  brandUse: 1,
});

//规则校验
const rules = reactive<FormRules>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandTop: [{ required: true, message: "请输入排序", trigger: "blur" }],
  brandStatus: [
    {
      required: true,
      message: "请选择品牌状态",
      trigger: "change",
    },
  ],
});

//修改状态
const getStatus = (row: any) => {
  updateStatus(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//添加校验
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      AddBrand();
    } else {
      console.log("error submit!", fields);
    }
  });
};

//添加品牌
const AddBrand = () => {
  getAddBrand(addForm.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

//显示添加框
const onAddBrand = () => {
  addForm.value = {
    brandId: null,
    brandName: "",
    brandStatus: null,
    brandTop: null,
    brandUse: 1,
  };
  dialogFormVisible.value = true;
};

//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadList();
};

const loadList = () => {
  getBrandList(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<style scoped></style>
